package com.ruike.alisurface.answerQuestion;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseFgt;
import com.ruike.alisurface.answerQuestion.adapter.QuestionOptionAdapter;
import com.ruike.alisurface.answerQuestion.bean.QuestionBean;
import com.ruike.alisurface.utils.MyCountDownTimer;

import java.util.List;
import java.util.Locale;
import java.util.Objects;

import butterknife.BindView;

/**
 * Author: voodoo
 * CreateDate: 2023/11/20 020下午 12:48
 * Description: 展示题目
 */
@SuppressLint("ValidFragment")
public class ShowQuestionFragment extends BaseFgt {

    @BindView(R.id.viewQuestionShow_questionNumber_tv)
    TextView questionNumberTv;
    @BindView(R.id.viewQuestionShow_pageCountdownTime_tv)
    TextView countdownTimeTv;
    @BindView(R.id.viewQuestionShow_questionContent_tv)
    TextView questionContentTv;
    @BindView(R.id.viewQuestionShow_questionOptionList_recv)
    RecyclerView questionOptionListRecv;

    QuestionBean bean;
    int countDownTime = 20; // 界面的倒计时
    QuestionOptionAdapter questionOptionAdapter;
    MyCountDownTimer countDownTimer;

    /**
     * 创建 Fragment
     *
     * @param questionBean  需要展示的题目
     * @param countDownTime 倒计时时间
     */
    public ShowQuestionFragment(QuestionBean questionBean, int countDownTime) {
        super();
        this.bean = questionBean;
        this.countDownTime = countDownTime <= 2 ? this.countDownTime : countDownTime;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_question_show;
    }

    @Override
    protected void initViews(View view) {
        String questionNumber = String.format(Locale.ENGLISH, "第 %d 题\t\t%s", bean.getQuestion_index(), bean.getQuestion_type_str());
        questionNumberTv.setText(questionNumber);
        questionContentTv.setText(bean.getQuestion_content());
        questionOptionListRecv.setLayoutManager(new LinearLayoutManager(getFragmentContext()));
        questionOptionAdapter = new QuestionOptionAdapter(getFragmentContext(), bean.getOptions(), bean.getQuestion_type() != 1);
        questionOptionListRecv.setAdapter(questionOptionAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            startTime();
        } else {
            stopTime();
        }
    }

    /**
     * 获取所选的答案
     *
     * @param isShowCorrectAnswers 是否展示正确答案
     * @return 选项列表
     */
    public List<QuestionBean.OptionsBean> getAnswerOptions(boolean isShowCorrectAnswers) {
        // 取出当前作答的整体列表
        List<QuestionBean.OptionsBean> currentOptions = bean.getOptions();
        // 如果需要显示答案
        if (isShowCorrectAnswers) {
            // 取出后台给的答案列表，并进行字节替换等
            String answer_value = bean.getAnswer_value()
                    .replace("[", "")
                    .replace("]", "")
                    .replace(",", "_")
                    .replace(" ", "");
            answer_value = "_" + answer_value + "_";
            // 取出作答选择的列表
            StringBuilder myAnswerValue = new StringBuilder();
            myAnswerValue.append("_");
            for (QuestionBean.OptionsBean options : currentOptions) {
                if (options.isSelect()) {
                    myAnswerValue.append(options.getSort());
                    myAnswerValue.append("_");
                }
            }

            System.out.println("第" + bean.getQuestion_index() + "题 ==== 题目中答案：" + answer_value + " ==== 选择的答案：" + myAnswerValue + " ==== " + (answer_value.contentEquals(myAnswerValue) ? "回答正确" : "回答错误"));

            // 此处处理选项的展示
            List<QuestionBean.OptionsBean> list = questionOptionAdapter.getList();
            for (int i = 0; i < list.size(); i++) {
                QuestionBean.OptionsBean optionsBean = list.get(i);
                if (optionsBean.isSelect()) {
                    optionsBean.setType(answer_value.contains("_" + optionsBean.getSort() + "_") ? 1 : -1);
                    currentOptions.set(i, optionsBean);
                } else {
                    if (answer_value.contains("_" + optionsBean.getSort() + "_")) {
                        optionsBean.setType(1);
                        currentOptions.set(i, optionsBean);
                    }
                }
            }
            questionOptionAdapter.setList(list);
        }
        return currentOptions;
    }

    public void startTime() {
        stopTime();
        countDownTimer = new MyCountDownTimer(countDownTime * 1000, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
                countdownTimeTv.setText(String.format(Locale.ENGLISH, "%ds", (millisUntilFinished+600) / 1000));
            }

            @Override
            public void onFinish() {
                ((AnswerQuestionActivity) Objects.requireNonNull(getActivity())).nextQuestion();
            }
        };
        countDownTimer.start();
    }

    public void stopTime() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    public TextView getCountdownTimeTv() {
        return countdownTimeTv;
    }

}
